<?php

	include("common.php");
	
	if(!empty($_POST)){
		mysql_query("INSERT INTO `197_users` ( `username`, `password`, `email`, `type`, `realname` ) VALUES ( '" . $_POST['username'] . "', '" . md5($_POST['password']) . "', '" . $_POST['email'] . "', " . $_POST['type'] . ", '" . $_POST['realname'] . "' )");
		include("header.php");
		
?>
<?php if(!AJAX){ ?><h1>新增帳號</h1><?php } ?>
<p>新增成功<?php if(!AJAX){ ?>，請<a href="userFind.php">按此</a>返回<?php } ?>。</p>
<?php

		include("sidebar.php");
		include("footer.php");
		exit;
		
	}

?>
<?php include("header.php"); ?>
<h1>新增帳號</h1>
<div id="add-result-dialog">
</div>
<script type="text/javascript" defer="defer">
	$("#add-result-dialog").dialog({
		width: 300,
		height: 210,
		autoOpen: false,
		title: "新增帳號",
		modal: true,
		draggable: false,
		resizable: false
	});
	function addUser(){
		$("#add-result-dialog").dialog("option", "buttons", { "關閉": function(){ location.replace("userFind.php"); } });
		$("#add-result-dialog").html("<img src='../images/ajax-loader.gif' />");
		$("#add-result-dialog").dialog("open");
		$.post("userAdd.php?ajax=true", { username: $("#username").val(), password: $("#password").val(), email: $("#email").val(), type: $("#type").val(), realname: $("#realname").val() }, function(r){ $("#add-result-dialog").html(r) });
	}
</script>
<form action="userAdd.php" onsubmit="addUser(); return false;" method="POST">
	<p><label>帳號：<input type="text" name="username" id="username" /></label></p>
	<p><label>密碼：<input type="password" name="password" id="password" /></label></p>
	<p><label>信箱：<input type="text" name="email" id="email" /></label></p>
	<p><label>姓名：<input type="text" name="realname" id="realname" /></label></p>
	<p><label>類型：<select name="type" id="type"><option value="1">一般</option><option value="2">管理員</option></select></label></p>
	<p><input type="submit" value="新增" /></p>
</form>
<?php include("sidebar.php"); ?>
<?php include("footer.php"); ?>